﻿var jsonData;
	var dashData;
	var curView=2;//1:列表模式  2：网格模式
	
	$(document).ready(request("a-1"));
	
	
	function request(path){
		$.ajax({       
			type: "POST",   
			url: "/resource/resource_list.do?path="+path, 
			contentType: "application/json; charset=utf-8", 
			dataType: "json",       
			data: "",  
			success: function(res) { 
				jsonData=res;
				$('#DynamicGridLoading').hide();
				refresh(curView);
			}   
			});
	}
	function switchView(view){
		curView=view;
		if(curView==1){
			$('#DynamicGrid').slideUp();
			$('#DynamicList').slideDown();
		}else if(curView==2){
			$('#DynamicList').slideUp();
			$('#DynamicGrid').slideDown();
		}
		
	}
	
	function refresh(view){
		curView=view;
		var item=document.getElementById("DynamicList");
		item.innerHTML=CreateTableView(jsonData.resources, "newspaper", true);
		item=document.getElementById("DynamicGrid");
		item.innerHTML=createGrid(jsonData.resources,jsonData.path);
		switchView(curView);
	}
	
	function del(rid,type,path){
		
		var flag=confirm("确定要删除文件？");
		if(!flag)
			return;
		
		for(var i=0;i<jsonData.resources.length;i++)
			if(jsonData.resources[i].rid==rid){
				jsonData.resources.splice(i,1);
				var xmlhttp=getXmlHttp();
				xmlhttp.open("GET", "/resource/delete.do?rid="+rid+"&type="+type+"&path="+path, true);
				xmlhttp.send();
				break;
			}
		refresh(curView);
		
	}
	

	function rename1(rid,pid){
		var item=document.getElementById(rid+"_name");
		var inputBox;
		if(item.innerHTML.indexOf("<input")!=-1){//防止重复点击“重命名”按钮
			inputBox=document.getElementById("input_"+rid);
			inputBox.select();
			inputBox.focuse();
			return;
		}
		item.innerHTML="<input id=\"input_"+rid+"\" type=\"text\" value=\""+item.innerHTML+"\" onkeydown=\"rename2(this.value,"+rid+","+pid+")\">";
		inputBox=document.getElementById("input_"+rid);
		inputBox.select();
		inputBox.focuse();
	}
	function rename2(name,rid,pid){
		if(event.keyCode==13&&name!=""){
			var item=document.getElementById(rid+"_name");
			item.innerHTML=name;//更新页面提升用户体验
			for(var i=0;i<jsonData.resources.length;i++)
				if(jsonData.resources[i].rid==rid){
					jsonData.resources[i].name=name;
					break;
				}
			//发送异步数据删除服务器数据
			var xmlhttp=getXmlHttp();
			xmlhttp.open("GET", "/resource/rename.do?rid="+rid+"&name="+encodeURI(encodeURI(name)), true);
			xmlhttp.send();
		}
	}
	function addFolder(path){
		var name=prompt("请输入文件夹名称","新建文件夹");
		if(name==null||name=="")
			return;
		$.ajax({       
			type: "GET", 
			async:false,
			url: "/resource/add_folder.do?path="+path+"&name="+encodeURI(encodeURI(name)), 
			contentType: "application/json; charset=utf-8", 
			dataType: "json",       
			data: "",  
			success: function(res) { 
				try{
					jsonData=res;
					refresh(curView);
				}catch(error){
					alert(error);
				}
			}   
			});
	}
	
	function shareFolder(rid,path){
		alert(rid+"\n"+path);
		
	}
	
	function getXmlHttp(){
		var xmlhttp;
		if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp = new XMLHttpRequest();
		} else {// code for IE6, IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		return xmlhttp;
	}
	function upload(){
		window.location.href="/resource/resource_add_view.do?path="+jsonData.path;
	}
	function show_resource(rid){
		//window.location.href="/jsp/resource/resource_show.jsp?rid="+rid;
		var top=document.getElementById("top");
		var item;
		for(var i=0;i<jsonData.resources.length;i++)
			if(jsonData.resources[i].rid==rid){
				item=jsonData.resources[i];
				break;
			}
		var str="<ul>";
		str+="<li>"+"<img src='/img/file_type/word.png' >"+"</li>"
		str+="<li>"+"名称："+item.name+"</li>"
		str+="<li>"+"类型："+item.type+"</li>"
		str+="<li>"+"大小："+item.size+"</li>"
		str+="<li>"+"上传者："+item.owner+"</li>"
		str+="<li>"+"上传时间："+item.create_time+"</li>"
		str+="<li>"+"下载次数："+item.download_count+"</li>"
		str+="<li>"+"<a href='<%=ResourceConfiguration.resource_downloadPath%>"+item.store_relative_path+"'>下载</a>"+"</li>"
		str+="</ul>";
		
		top.innerHTML=str;
		
	}
	//在线预览
	function show_resource_online(rid){
		//window.location.href="/jsp/resource/resource_show.jsp?rid="+rid;
		$(".mainRightBar").remove();
		$(".divPage").remove();
		var content=$(".content");
		var item;
		for(var i=0;i<jsonData.resources.length;i++)
			if(jsonData.resources[i].rid==rid){
				item=jsonData.resources[i];
				break;
			}
		content.html('<a id="viewerPlaceHolder" style="width:680px;height:600px;display:block"></a>');
		var str=item.store_relative_path
		str=str.replace("/source/","/swf/");
		str=str.substring(0,str.lastIndexOf("."));
		str=str+".swf";
		
		var fp = new FlexPaperViewer(	
				 '../../static/flexpaper/FlexPaperViewer',
				 'viewerPlaceHolder',
				 { config : {
		             SwfFile : escape('http://localhost:8080/g305_download/'+str),
		             Scale : 1.2,
		             ZoomTransition : "easeOut",
		             ZoomTime : 0.5,
		             ZoomInterval : 0.1,
		             FitPageOnLoad : false,
		             FitWidthOnLoad : false,
		             FullScreenAsMaxWindow : false,
		             ProgressiveLoading : true,
		             MinZoomSize : 0.2,
		             MaxZoomSize : 5,
		             SearchMatchAll : false,
		             InitViewMode : 'Portrait',
		                    
		             ViewModeToolsVisible : true,
		             ZoomToolsVisible : true,
		             NavToolsVisible : true,
		             CursorToolsVisible : true,
		             SearchToolsVisible : true,  
		             localeChain : "en_US"
		   }});
	}
	function dash(){
		$.ajax({       
			type: "GET", 
			async:false,
			url: "/resource/get_droped_resources.do", 
			contentType: "application/json; charset=utf-8", 
			dataType: "json",       
			data: "",  
			success: function(res) { 
				dashData=res;
				try{
					showDashView(dashData.resources);
				}catch(error){
					alert(error);
				}
			}   
			});
	}
	function restore(rid,path){	
		for(var i=0;i<dashData.resources.length;i++)
			if(dashData.resources[i].rid==rid){
				var item=dashData.resources[i];
				if(item.path==jsonData.path)
					jsonData.resources.push(dashData.resources[i]);//恢复
				dashData.resources.splice(i,1);
				break;
			}
		showDashView(dashData.resources);
		
		var xmlhttp=getXmlHttp();
		xmlhttp.open("GET", "/resource/restore.do?path="+path+"a"+rid);
		xmlhttp.send();
	}
	function showDashView(objArray){
		$('#DynamicGrid').slideUp();
		$('#DynamicList').slideUp();
		var str="";
		str+='<a href="#" onclick="clearDash();">清空回收站</a>|\
			<a href="#" onclick="backFromDashToList()">返回</a>';
		str+='<table><tbody><tr>';
		 var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
		 for (var i = 0; i < array.length; i++) {
		    	var item=array[i];
		    	if(item.type==4){
		    		str+='<td align="center">\
		    			<p>\
						<img id="img_type" src="../../static/img/file_type/folder.png"  >\
						</p>\
						<p id="'+item.rid+'_name"> '+item.name+'</p>\
						<p id="'+item.rid+'_name"> '+item.last_modify_time+'</p>\
						<a href="#" onclick="restore(\''+item.rid+'\',\''+item.path+'\')">还原</a>\
						</td>';
				
		    	}else{
		    		str+='<td>\
						<img id="img_type" src="../../static/img/file_type/word.png" align="left" >\
						名称： '+item.name+'<br/>\
						大小：'+item.size+'<br/>\
						 类别：程序文档<br/> \
		    			上传者：'+item.owner+'<br/>\
		    			<a href="#" onclick="restore(\''+item.rid+'\',\''+item.path+'\')">还原</a>\
					</td>';
		    	}
		    	if((i+1)%4==0){
		    		str+='</tr><tr>';
		    	}
		    }
		
			str+='</tr></tbody></table>';
			var dashView=document.getElementById("Dash");
			dashView.innerHTML=str;
			
			$("#Dash").slideDown();
		
	}
	function backFromDashToList(){
		$("#Dash").slideUp();
		refresh(curView);
	}
	function clearDash(){
		var flag = confirm("确定要清空回收站？")
		if(!flag)
			return;
		dashData.resources.length=0;//清空数组
		$.ajax({       
			type: "GET", 
			async:false,
			url: "/resource/clear_dash.do", 
			contentType: "application/json; charset=utf-8", 
			dataType: "json",       
			data: "",  
			success: function(res) { 
			}   
			});
		showDashView(dashData);
		
	}
	